package com.tsn.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsn.common.Dto.AttentionDto;
import com.tsn.common.Result;
import com.tsn.entity.Attention;
import com.tsn.entity.Comment;
import com.tsn.entity.User;
import com.tsn.service.AttentionService;
import com.tsn.service.UserService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 关注收藏表 前端控制器
 * </p>
 *
 * @author tsn
 * @since 2021-09-07
 */
@RestController
@RequestMapping("/tsn/attention")
public class AttentionController {
    @Autowired
    private AttentionService attentionService;
    @Autowired
    private UserService userService;
    @GetMapping("/getMyAttentions")
    public Result myAttentionList(@RequestParam("userId")Integer userId){
        return Result.success("查询成功",attentionService.list());
    }
    @GetMapping("/getMyAANdF")
    public Result getMyInfo(@RequestParam("userId")Integer userId){
        return attentionService.getMyNumber(userId);
    }

    @GetMapping("/getUserAttention")
    public Result getUserAttention(@RequestParam("userId")Integer userId,@RequestParam("attentionId")Integer attentionId,@RequestParam("type")Integer type){
        QueryWrapper<Attention> attentionQueryWrapper = new QueryWrapper<>();
        attentionQueryWrapper.eq("user_id",userId);
        attentionQueryWrapper.eq("attention_id",attentionId);
        attentionQueryWrapper.eq("type",type);
        Attention one = attentionService.getOne(attentionQueryWrapper);
        if(one == null){
            return Result.failure(null);
        }
        return Result.success(one.getId());
    }
    @PostMapping("/addAttention")
    public Result addAttention(@RequestBody JSONObject object){
        Attention attention = JSON.toJavaObject(object, Attention.class);
        if(attention.getAttentionId()==null){
//            QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>();
            User userInfo = userService.getUserInfo(attention.getUserName());
            attention.setAttentionId(userInfo.getId());
        }
        attentionService.save(attention);
        return Result.success(null);
    }
    @GetMapping ("/deleteAttention")
    public Result deleteAttention(@RequestParam("id")Integer id){
        attentionService.removeById(id);
        return Result.success(null);
    }
    @GetMapping("/getUserFollow")
    public Result getUserFollow(@RequestParam("userId")Integer userId,@RequestParam("userName")String userName){
        System.out.println(userId+userName);
        User userInfo = userService.getUserInfo(userName);

        QueryWrapper<Attention> attentionQueryWrapper = new QueryWrapper<>();
        attentionQueryWrapper.eq("user_id",userId);
        attentionQueryWrapper.eq("attention_id",userInfo.getId());
        attentionQueryWrapper.eq("type",0);
        Attention one = attentionService.getOne(attentionQueryWrapper);
        if(one == null){
            return Result.failure(null);
        }
        return Result.success(one.getId());
    }
    @RequiresAuthentication
    @GetMapping("/listMyAttention")
    public Result listMyAttention(@RequestParam("id")Integer id){
        return attentionService.listMyAttention(id);
    }
    @RequiresAuthentication
    @GetMapping("/listAttentionMe")
    public Result listAttentionMe(@RequestParam("id")Integer id){
        return attentionService.listAttentionMe(id);
    }
}
